************************************************
* Sleep Project - Pedro Bessone, Gautam Rao, Heather Schofield, Frank Schilbach, and Mattie Toma
* Purpose: Replicates Appendix Table 19 (Relationship between Present Bias (β) and Behaviors Involving Time Preference)
* Last edited: 07 May 2021
************************************************

	use "$d/pb_dataset.dta", clear

	*Restrict sample to "censored" sample
		keep if type == "censored"
		keep if convergence_code_post == 0
		replace beta_post = 2 if beta_post > 2 & beta_post != .
		replace beta_base = 2 if beta_base > 2 & beta_base != .

		sum beta_base

		gen dummy_missing = (beta_base==.)

		replace beta_base = r(mean) if beta_base==.

	gen beta_raw_r = .
	gen beta_str_r = .
	label var beta_raw_r "Beta RF"	
	label var beta_str_r "Beta Structural"
	
	eststo clear
	foreach var in deposits lateness1 typing_time_hr sleep_night {
		
		replace beta_str_r = beta_post	

			reg `var' beta_str_r if convergence_code_post == 0, vce(cluster pid)
			
		eststo
			qui sum `var' if treat_pool ==0 & treat_nap == 0
			estadd scalar Cmean = r(mean)
			estadd scalar Csd = r(sd)
			
			reg `var' beta_str_r female i.age if convergence_code_post == 0, vce(cluster pid)
			
		eststo
			qui sum `var' if treat_pool ==0 & treat_nap == 0
			estadd scalar Cmean = r(mean)
			estadd scalar Csd = r(sd)
			
	}	
		** Exporting
		esttab using "$oa/Tables/TableA19_pb_correlations.tex", replace ///
		se lab stats(Cmean Csd N, labels("Control Mean" "Control SD" "Observations") fmt(%9.2f %9.2f %9.0f)) ///
		star( * 0.10 ** 0.05 *** 0.01) varwidth(32) wrap compress nogap noconstant ///
		nonumbers nonotes booktabs b(2)  keep(beta_str_r) ///
		indicate("Controls = female *age*") ///
		mgroups("Daily Deposits" "Lateness" "Typing Time" "Night Sleep", ///
		pattern(1 0 1 0 1 0 1 0) span prefix(\multicolumn{@span}{c}{) suffix(}) ///
		erepeat(\cmidrule(lr){@span})) ///
		mtitles("(1)" "(2)" "(3)" "(4)" "(5)" "(6)" "(7)" "(8)")
